package org.mch.comm.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.mch.comm.exec.MsgException;

/**
 * 描述：常用工具类；<BR/>
 * 
 * @author ZL；<BR/>
 *
 *         参考：MCHWEB.NET；<BR/>
 *
 */
public class MTools {

	public static SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");

	/**
	 * 检查对象是否为空，如果为空则抛出MsgException异常；
	 * 
	 * @param obj
	 *            ：检查对象；
	 * @param msg
	 *            ：异常消息；
	 */
	public void checkNull(Object obj, String msg) {
		if (obj == null || obj.equals("")) {
			throw new MsgException(msg);
		}
	}

	/**
	 * 给文件名增加后缀字符串；
	 * 
	 * @param filename
	 * @param suffix
	 * @return
	 */
	public String fileNameAddSuffix(String filename, String suffix) {
		if (filename == null || filename.equals("") || suffix == null || suffix.equals("")) {
			return filename;
		} else {
			int dindex = filename.lastIndexOf(".");
			return filename.substring(0, dindex) + suffix + filename.substring(dindex + 1, filename.length());
		}
	}

	public boolean isNull(Object obj) {
		if (obj == null) {
			return true;
		}
		if (obj.toString().trim().equals("")) {
			return true;
		}
		return false;
	}

	public boolean isNull(Object... objs) {
		for (int i = 0, len = objs.length; i < len; i++) {
			Object obj = objs[i];
			if (obj == null) {
				return true;
			}
			if (obj.toString().trim().equals("")) {
				return true;
			}
		}
		return false;
	}

	public boolean notNull(Object obj) {
		if (obj != null && !obj.toString().trim().equals("")) {
			return true;
		}
		return false;
	}

	public String getCurDate1() {
		return format1.format(new Date());
	}

	public Object[][] getObjObj(List<Object[]> list) {
		Object[][] objobj = new Object[list.size()][];
		for (int i = 0, len = list.size(); i < len; i++) {
			objobj[i] = list.get(i);
		}
		return objobj;
	}

	public String toStr(Object obj) {
		if (obj == null) {
			return null;
		} else {
			if (obj instanceof String) {
				return (String) obj;
			} else {
				return obj.toString();
			}
		}
	}

	private Random random = new Random();

	/**
	 * 随机一个小于 withnumber 的正整数；
	 * 
	 * @param withnumber
	 * @return
	 */
	public int randomAbsInt(int withNumber) {
		int i = Math.abs(random.nextInt());
		return i % withNumber;
	}

	/**
	 * 将字符串 s 转换为int返回，如果错误，则返回默认值；
	 * 
	 * @param s
	 * @param def
	 * @return
	 */
	public int parseInt(String s, int def) {
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
			return def;
		}
	}

	public void pause(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
		}
	}

	public void pause1() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
	}

	public void pause2() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
		}
	}

	public Class<?> getObjClass(String className) {
		try {
			return Class.forName(className);
		} catch (Exception e) {
			throw new MsgException("加载[" + className + "]类失败，请检查。", e);
		}
	}

	public Object newObj(String className) {
		try {
			return Class.forName(className).newInstance();
		} catch (Exception e) {
			throw new MsgException("创建[" + className + "]对象失败，请检查。", e);
		}
	}

	public Object newObj(Class<?> clazz) {
		try {
			return clazz.newInstance();
		} catch (Exception e) {
			throw new MsgException("创建[" + clazz.getName() + "]对象失败，请检查。", e);
		}
	}

	public String firstLetterUpperCase(String str) {
		return str.substring(0, 1).toUpperCase() + str.substring(1, str.length());
	}

	public long getSerialVersionUID() {
		return new Random().nextLong();
	}

	public String getListLine(List<String> list) {
		StringBuilder sb = new StringBuilder();
		Iterator<String> iter = list.iterator();
		while (iter.hasNext()) {
			sb.append(iter.next()).append("\n");
		}
		return sb.toString();
	}
}
